package com.tongji.housediy.service
{
	import com.tongji.housediy.events.ChangeStateEvent;
	import com.tongji.housediy.events.designEvent.GetDesignsByUserEvent;
	import com.tongji.housediy.events.designEvent.SaveDeleteDesignEvent;
	import com.tongji.housediy.model.DesignModel;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.utils.PopUpAdmin;
	import com.tongji.housediy.vo.Design;
	
	import flash.system.Security;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/4/11
	 */ 
	public class DesignService extends Actor
	{
		public var houseRemoteObject:RemoteObject;
		
		[Inject]
		public var designModel:DesignModel;
		
		[Inject]
		public var userModel:UserInforModel;
		
		public function DesignService()
		{
			super();
		    //var amfChannel:Array = [new AMFChannel(null, "http://10.60.0.118/housediy/Gateway.aspx")];
		   var amfChannel:Array = [new AMFChannel(null, "http://design.rryjw.com/Gateway.aspx")];
			 //var amfChannel:Array = [new AMFChannel(null, "http://localhost/housediy/Gateway.aspx")];

			//var amfChannel:Array = [new AMFChannel(null, "http://ssegrc.gotoip55.com/Gateway.aspx")];
			var channelSet:ChannelSet = new ChannelSet();
			channelSet.channels = amfChannel;
			Security.loadPolicyFile("http://design.rryjw.com/crossdomain.xml");

			houseRemoteObject = new RemoteObject("fluorine");
			houseRemoteObject.source = "HouseDiyService.HouseDiyAction";
			houseRemoteObject.showBusyCursor = true;
			houseRemoteObject.channelSet = channelSet;
			
			houseRemoteObject.getOperation("saveDesign").addEventListener(ResultEvent.RESULT, saveDesignResultHandler);
			houseRemoteObject.getOperation("saveDesign").addEventListener(FaultEvent.FAULT, faultHandler);
			
			houseRemoteObject.getOperation("getDesignsByUserID").addEventListener(ResultEvent.RESULT, getDesignsResultHandler);
			houseRemoteObject.getOperation("getDesignsByUserID").addEventListener(FaultEvent.FAULT, faultHandler);
			
			houseRemoteObject.getOperation("deleteDesignByID").addEventListener(ResultEvent.RESULT, deleteDesignResultHandler);
			houseRemoteObject.getOperation("deleteDesignByID").addEventListener(FaultEvent.FAULT, faultHandler);
		}
		
		public function saveDesignByUser(design:Design):void
		{
			houseRemoteObject.saveDesign(design);
		}
		
		public function getDesignsByUserID(userID:String, pageIndex:int, itemCount:int):void
		{
			houseRemoteObject.getDesignsByUserID(userID, pageIndex, itemCount);
		}
		
		public function deleteDesign(design:Design):void
		{
			houseRemoteObject.deleteDesignByID(design);
		}
		
		/*---------------------返回值处理函数-----------------------------------*/	
        private function saveDesignResultHandler(event:ResultEvent):void
		{
			
			if(event.result)
			{
				Alert.show("保存成功");
				dispatch(new ChangeStateEvent(ChangeStateEvent.ROOM_OPENED));
				getDesignsByUserID(userModel.userID, 1, 8);
			}
			//dispatch(new SaveOpenDesignEvent(SaveOpenDesignEvent.SAVE_DESIGN_RESULT, null, event.result as ArrayCollection));
		}
		
		private function getDesignsResultHandler(event:ResultEvent):void
		{
			dispatch(new GetDesignsByUserEvent(GetDesignsByUserEvent.UPDATE_DESIGN_MODEL, 0, 0,null, event.result as ArrayCollection));
		}
		
		private function deleteDesignResultHandler(event:ResultEvent):void
		{
			dispatch(new GetDesignsByUserEvent(GetDesignsByUserEvent.UPDATE_DESIGN_MODEL, 0, 0,null, event.result as ArrayCollection));
		}
		
		private function faultHandler(event:FaultEvent):void
		{
			Alert.show(" fault :" + event.toString());
		}
	}
}